package com.xingkong.web.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xingkong.common.annotation.Log;
import com.xingkong.common.enums.BusinessType;
import com.xingkong.product.domain.SpecificationsItem;
import com.xingkong.product.service.ISpecificationsItemService;
import com.xingkong.common.core.controller.BaseController;
import com.xingkong.common.core.text.Convert;
import com.xingkong.common.core.domain.AjaxResult;
import com.xingkong.common.utils.poi.ExcelUtil;
import com.xingkong.common.core.page.TableDataInfo;

/**
 * 规格项值Controller
 *
 * @author yiyue
 * @date 2020-04-06
 */
@Controller
@RequestMapping("/product/item")
public class SpecificationsItemController extends BaseController {
    private String prefix = "product/item";

    @Autowired
    private ISpecificationsItemService specificationsItemService;

    @RequiresPermissions("product:item:view")
    @GetMapping()
    public String item() {
        return prefix + "/item";
    }

            /**
         * 查询规格项值列表
         */
        @RequiresPermissions("product:item:list")
        @PostMapping("/list")
        @ResponseBody
        public TableDataInfo list(SpecificationsItem specificationsItem) {
            startPage();
            return getDataTable(specificationsItemService.list(new QueryWrapper<SpecificationsItem>(specificationsItem)));
        }
    
    /**
     * 导出规格项值列表
     */
    @RequiresPermissions("product:item:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SpecificationsItem specificationsItem) {
        List<SpecificationsItem> list = specificationsItemService.list(new QueryWrapper<>());
        ExcelUtil<SpecificationsItem> util = new ExcelUtil<SpecificationsItem>(SpecificationsItem. class);
        return util.exportExcel(list, "item");
    }

            /**
         * 新增规格项值
         */
        @GetMapping("/add/{specificationsId}")
        public String add(@PathVariable("specificationsId") Long specificationsId, ModelMap mmap) {
            mmap.put("specificationsId",specificationsId);
            return prefix + "/add";
        }
    
    /**
     * 新增保存规格项值
     */
    @RequiresPermissions("product:item:add")
    @Log(title = "规格项值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SpecificationsItem specificationsItem) {
        return toAjax(specificationsItemService.save(specificationsItem));
    }

    /**
     * 修改规格项值
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SpecificationsItem specificationsItem =specificationsItemService.getById(id);
        mmap.put("specificationsItem", specificationsItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存规格项值
     */
    @RequiresPermissions("product:item:edit")
    @Log(title = "规格项值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SpecificationsItem specificationsItem) {
        return toAjax(specificationsItemService.updateById(specificationsItem));
    }

            /**
         * 删除规格项值
         */
        @RequiresPermissions("product:item:remove")
        @Log(title = "规格项值", businessType = BusinessType.DELETE)
        @PostMapping("/remove")
        @ResponseBody
        public AjaxResult remove(String ids) {
            return toAjax(specificationsItemService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
        }
        }
